#include "scbitf.h"
#include "robohubscb/robohub.h"
#include "pscb/pscb.h"
// #include "hscb/hscb.h"
#include "log/zlog.hh"

rtdev::SafetyBoardItf* rtdev::SafetyBoardItf::create(uint8_t scbtype)
{
    std::unique_ptr<rtdev::SafetyBoardFactoryItf> factory;
    switch (scbtype)
    {
    case kScbTypeMinicab:
        return new rtdev::PScb();
    // case kScbTypeRoboHub:
    //     return new rtdev::RoboHubScb();
    //     break;
    // case kScbTypeMaxcab:
    //     return new rtdev::HScb();
    case kScbTypeRoboHub:
        return new rtdev::RoboHubScb();
    default:
        return nullptr;
    };
    rtdev::SafetyBoardItf* ret = factory->create();

    return ret;
}

rtdev::SafetyBoardItf::SafetyBoardItf() : sim_flag_(0), alive_watchdog_(0), alive_flag_(false), last_alive_cnt_(-1), default_watchdog_(1000) {}
int rtdev::SafetyBoardItf::shutdown() { return 0; }
void rtdev::SafetyBoardItf::check_alive()
{
    bool alive = false;
    if (sim_flag_)
    {
        alive = true;
    }
    else
    {
        if (alive_watchdog_ <= 0)
        {
            alive = false;
        }
        else
        {
            alive_watchdog_--;
            alive = true;
        }
    }
    if (alive != alive_flag_)
    {
        zlog()->info("{} is {}, sim={}", name(), alive ? "online" : "offline", sim_flag_);
        alive_flag_ = alive;
    }
}
int rtdev::SafetyBoardItf::keep_alive(int alive)
{
    if (last_alive_cnt_ != alive)
    {
        last_alive_cnt_ = alive;
        alive_watchdog_ = default_watchdog_;
        return 0;
    }
    return -1;
}
bool rtdev::SafetyBoardItf::is_alive()
{
    if (sim_flag_)
    {
        return true;
    }
    return alive_flag_;
}
bool rtdev::SafetyBoardItf::is_sim() { return sim_flag_ == 1; }